package view.transitions;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class RandomBarTransition extends Animator {
	public static final int	MIN_MOSAIC_SIZE	= 20;
	private int				mosaic_size;
	private BufferedImage	buffer;
	private int				numSquaresHorizontal;
	private int[]			progress;

	public RandomBarTransition(BufferedImage from, BufferedImage to) {
		super(from, new BufferedImage(to.getWidth(), to.getHeight(), BufferedImage.TYPE_INT_ARGB));
		this.buffer = to;
		this.mosaic_size = Math.min(this.overlay.getWidth(), this.overlay.getHeight()) / 10;
		if (MIN_MOSAIC_SIZE > this.mosaic_size) {
			this.mosaic_size = MIN_MOSAIC_SIZE;
		}
		this.numSquaresHorizontal = this.overlay.getHeight() / this.mosaic_size
				+ ((this.overlay.getHeight() % this.mosaic_size == 0) ? 0 : 1);
		this.progress = new int[this.numSquaresHorizontal];
	}

	@Override
	public void animate() {
		this.currentFrame++;
		Graphics2D g2d = (Graphics2D) this.overlay.getGraphics();
		int x1, y1, x2, y2;
		for (int i = 0; i < this.progress.length; i++) {
			int width = this.overlay.getWidth();
			if (this.progress[i] >= width) {
				continue;
			}

			y1 = i * this.mosaic_size;
			y2 = y1 + this.mosaic_size;
			x1 = this.progress[i];
			x2 = Math.min(width, this.progress[i] + this.mosaic_size * (int) Math.round(Math.random()));
			g2d.drawImage(this.buffer, x1, y1, x2, y2, x1, y1, x2, y2, null);
			this.progress[i] = x2;
		}

	}
}
